<?php
$template = "config-theme";
$reload=false;

function cleanString($string){
    $string = str_replace('http://', '', $string);
    if(substr($string, -1) === '/') $string = substr ($string, 0, -1);
    return $string;
}

// récupération des données en cours
if(DEBUG_MODE && file_exists(CONFIG_PATH.'config_local.ini')){
    $datas = parse_ini_file(CONFIG_PATH.'config_local.ini',true);
}elseif(!DEBUG_MODE && file_exists(CONFIG_PATH.'config.ini')){
    $datas = parse_ini_file(CONFIG_PATH.'config.ini',true);
}else{
    exit;
}

// suvegarde des données en tmp
if(isset($_POST)&&isset($_POST['submit'])){
    $datas['domaine']['siteName'] = $_POST['domaine_siteName'];
    $datas['design']['theme'] = $_POST['design_theme'];
    $datas['design']['theme_ui'] = $_POST['design_theme_ui'];
    $datas['design']['logo_media_id'] = $_POST['design_logo_media_id'];
    
    $config_ini = '';
    foreach($datas as $rub=>$content){
        $config_ini .= '['.$rub.']'."\n";
        foreach($content as $k=>$v){
            $config_ini .= $k.' = "'.$v.'"'."\n";
        }
    }
    
    $fileName = (DEBUG_MODE)?'config_local.ini':'config.ini';
    $handle = fopen(CONFIG_PATH.$fileName,"w+");
    fwrite($handle, $config_ini);
    fclose($handle);
    
    $reload = true;
}

// matching des données du .ini dans le formulaire
if(isset($datas)){
    foreach($datas as $rub=>$content){
        if($rub === 'langues'){
            $_POST[$rub]=$content;
        }else{
            foreach($content as $k=>$v){
                $_POST[$rub.'_'.$k] = cleanString($v);
            }
        }
    }
}

if($reload){
    header('location: ?template='.$template);
    exit;
}
?>
<div class="columns sixteen">
    <form method="post" enctype="multipart/form-data">
        <table class="liste ui-widget" width="100%">
            <thead class="ui-widget-header">
                <tr>
                    <th colspan="3">Thème</th>
                </tr>
            </thead>
            <tbody class="ui-widget-content">
                <tr>
                    <td>Nom du site</td>
                    <td><input type="text" name="domaine_siteName" value="<?php echo (isset($_POST)&&isset($_POST['domaine_siteName']))?$_POST['domaine_siteName']:''; ?>"/></td>
                    <td rowspan="3" width="25%">
                        <div style="margin: 5px 25px; padding: 15px 8px; background-color: #F2F2F2;">
                        <?php
                            $logo_media_id = (isset($_POST) && isset($_POST['design_logo_media_id']) && $_POST['design_logo_media_id']>0)?$_POST['design_logo_media_id']:0;
                            include('inc/mediaManager.inc.php');
                        ?>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>Thème du site</td>
                    <td>
                        <select name="design_theme">
                            <option value="default">default</option>
                            <?php
                            if($handle = opendir('../themes')){
                                while (false !== ($entry = readdir($handle))) {
                                    if ($entry != "." && $entry != ".." && !is_file('../themes/'.$entry)){
                                        if($entry!=='default'){
                                            $selected = (isset($_POST) && isset($_POST['design_theme']) && $entry === $_POST['design_theme'])?' selected="selected"':'';
                                            echo "\t".'<option value="'.$entry.'"'.$selected.'>'.$entry.'</option>'."\r\n";
                                        }
                                    }
                                }
                                closedir($handle);
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Thème du back office</td>
                    <td>
                        <select name="design_theme_ui">
                            <?php
                            if($handle = opendir('../css/themes')){
                                $currentTheme = (isset($_POST) && isset($_POST['design_theme_ui']))?$_POST['design_theme_ui']:THEME_UI_DEFAULT;
                                while (false !== ($entry = readdir($handle))) {
                                    if ($entry != "." && $entry != ".." && !is_file('../css/themes/'.$entry)) {
                                        $selected = ($entry===$currentTheme)?' selected="selected"':'';
                                        echo "\t".'<option value="'.$entry.'"'.$selected.'>'.$entry.'</option>'."\r\n";
                                    }
                                }
                                closedir($handle);
                            }
                            ?>
                        </select>
                    </td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="3" align="right"><input class="pwe-ui-btn" type="submit" name="submit" value="Enregistrer" /></td>
                </tr>
            </tfoot>
        </table>
    </form>
</div>